over 6 years ago

最近開近混用swift和objective-c了, 但是一遇到swift的程式碼,YouComplete就開始出現各種錯誤。
只好再重新設定.ycm_extra_conf.py
先從簡單的範例程式開始

#import "ViewController.h"
#import "objcVim-swift.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    hi* a = [[hi alloc] init];
}

@end

要在objective-c中使用的swift程式碼

import Foundation

@objc class hi: NSObject {
     override init(){
        super.init()
    }
}

YouCompleteMe出現錯誤訊息

objcVim/objcVim-swift.h|9 col 10 error| could not build module 'ObjectiveC'
objcVim/ViewController.m|18 col 6 error| 'ViewController' cannot use 'super' because it is a root class
objcVim/ViewController.m|20 col 18 error| no known class method for selector 'alloc'
objcVim/ViewController.m|24 col 6 error| 'ViewController' cannot use 'super' because it is a root class
objcVim/ViewController.h|11 col 12 warning| class 'ViewController' defined without specifying a base class

經過上次的教訓,決定先看YouComplete是不是又動了什麼手腳
:YcmDebug

-- ['-isystem', '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1',
 '-isystem', '/Library/Developer/CommandLineTools/usr/include/c++/v1', ...]

果然又出現一堆莫名其妙的flags

經過上次的教訓,直接找到罪魁禍首
~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/completers/cpp/flags.py

def _ExtraClangFlags():
  flags = _SpecialClangIncludes()
   if OnMac():
     for path in MAC_INCLUDE_PATHS:
       flags.extend( [ '-isystem', path ] )

YouCompleteMe果然又偷加clang flags.
把這一段程式直接移掉

:YcmDiag

objcVim/ViewController.h|9 col 9 error| could not build module 'UIKit'
objcVim/ViewController.m|18 col 6 error| 'ViewController' cannot use 'super' because it is a root class
objcVim/ViewController.h|11 col 12 warning| class 'ViewController' defined without specifying a base class

錯誤訊息變了,但還是不行
.ycm_extra_conf.py裡的flags砍到剩下

flags = [
'-isysroot',
'/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk',
]

再試一次:YcmDiag
出現

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/CoreFoundation.h |12 col 10 error| 'stdarg.h' file not found

用clang直接測試一下

clang -c -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk ViewController.m

沒問題呀
查一下上次寫的文章,發現少了

'-I/Library/Developer/CommandLineTools/usr/bin/../lib/clang/7.0.2/include',

加入這一行之後

flags = [
'-isysroot',
'/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk',
'-I/Library/Developer/CommandLineTools/usr/bin/../lib/clang/7.0.2/include',
]

:YcmDiag

No warnings or errors detected 

搞定收工

← Get Kong折騰筆記 使用libClang解析程式碼中的錯誤 →
 
comments powered by Disqus